import parole
from parole.colornames import colors
from parole.display import interpolateRGB
import pygame, random

import sim, sim_items, main, dungeon
from util import *

class Scroll(sim_items.Scroll):
    def __init__(self):
        sim_items.Scroll.__init__(self, "covetousness", "?", 
                ('light', 'have', 'far'),
                idDescription="When intoned, the clipped words contained in "\
                    "this scroll cause all objects near it to emit light. "\
                    "This prayer scroll was originally created by the priests "\
                    "of Khazalid, who would use it to bask in the glow of "\
                    "their wealth amassed from tithes.")

    def read(self, reader):
        # iterate through every tile of the reader's map, adding lightsources
        # wherever there are items, with the lightsource intensity proportional
        # to the weight of the item
        self.lightsources = []
        self.map = reader.parentTile.map
        playerSawIt = False
        for tile in self.map:
            for obj in tile:
                if isinstance(obj, sim_items.Item):
                    rgb = random.choice(['Red', 'Crimson', 'Pink', 'DeepPink',
                        'SpringGreen', 'Teal', 'Green', 'Lime', 'White',
                        'PaleTurquoise', 'Cyan'])
                    intensity = float(obj.weight)
                    ls = parole.map.LightSource(colors[rgb], intensity)
                    ls.apply(self.map, (tile.col, tile.row))
                    self.lightsources.append(ls)
                    if visible(tile):
                        playerSawIt = True
        self.map.update()

        main.schedule.add(sim.Event('expire effect', main.schedule.time + 2*60000,
            (self,), dests=[self]))

        # identification
        if playerSawIt:
            main.player.identify(self)
        elif reader is main.player:
            main.transient("Nothing seems to happen.")

    def listen(self, event):
        sim_items.Scroll.listen(self, event)

        if event.id == 'expire effect' and event.args[0] is self:
            for ls in self.lightsources:
                ls.remove(self.map)
            self.map.update()


#========================================
thingClass = Scroll

